// RecordAdapter.java
package com.example.qrcodecamera;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.ViewHolder> {

    private final List<WeightRecord> records;

    public RecordAdapter(List<WeightRecord> records) {
        this.records = records;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_record, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WeightRecord record = records.get(position);

        holder.tvDate.setText("日期: " + record.getDate());
        holder.tvBatch.setText("物料批次: " + record.getBatchNumber());
        holder.tvNumber.setText("物料编号: " + record.getNumber());
        holder.tvWeight1.setText("总重量: " + record.getWeight1() + "kg");
        holder.tvWeight2.setText("皮重: " + record.getWeight2() + "kg");
        holder.tvWeight3.setText("净重: " + record.getWeight3() + "kg");
    }

    @Override
    public int getItemCount() {
        return records.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvDate, tvBatch, tvNumber, tvWeight1, tvWeight2, tvWeight3;

        public ViewHolder(View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvBatch = itemView.findViewById(R.id.tv_batch);
            tvNumber = itemView.findViewById(R.id.tv_number);
            tvWeight1 = itemView.findViewById(R.id.tv_weight1);
            tvWeight2 = itemView.findViewById(R.id.tv_weight2);
            tvWeight3 = itemView.findViewById(R.id.tv_weight3);
        }
    }
}